using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;


namespace MapEditor
{
    public class Game
    {
        public bool SquadSelected;
        public int NumberOfPlayers;
        public int PlayerID = 0;

        public Company[] Companies;
        public GraphicsEngine GraphicsEngine;
        
        public bool GivingOrder;


        public Game() 
            //constructor
        {
            GraphicsEngine = new GraphicsEngine();

        }

        public bool Start()
        {
            return true;

        }

        public bool CreateCompanies(int numPlayers)
        //creates a company for each player
        {
            Companies = new Company[numPlayers];

            for (int looper = 0; looper < numPlayers; looper++)
            {
                Companies[looper] = new Company();
                //loop through each player, and create 1 squad of 1 soldier
                Companies[looper].PlayerID = looper;
                Companies[looper].Squads = new Squad[10];
            }
            //Companies = new Squad();
            Random rnd = new Random(100);

            //go through all companies
            for (int companyLooper = 0; companyLooper < Companies.Length; companyLooper++)
            {
                //then go through all the squads
                for (int squadLooper = 0; squadLooper < Companies[companyLooper].Squads.Length; squadLooper++)
                {
                    //then go through all the soldiers
                    Companies[companyLooper].Squads[squadLooper] = new Squad();
                    //Companies[companyLooper].Squads[squadLooper].Soldiers.Length
                    Companies[companyLooper].Squads[squadLooper].Soldiers = new Soldier[10];
                    for (int looper = 0; looper < Companies[companyLooper].Squads[squadLooper].Soldiers.Length; looper++)
                    {
                        Companies[companyLooper].Squads[squadLooper].Soldiers[looper] = new Soldier();
                        Companies[companyLooper].Squads[squadLooper].Soldiers[looper].Create(rnd.Next(800), rnd.Next(800), 0);
                    }
                }
            }
            return true;

        }

                
#region Soldier Routines

        public bool DoThinking()
        {
            //go through all companies
            for (int companyLooper = 0; companyLooper < Companies.Length; companyLooper++)
            {
                //then go through all the squads
                for (int squadLooper = 0; squadLooper < Companies[companyLooper].Squads.Length; squadLooper++)
                {
                    Companies[companyLooper].Squads[squadLooper].CarryOutOrders();
                }
            }
            return true;
        }
        public int SelectSquad(int GameX, int GameY)
        //have we selected a squad?
        {

            for (int squadLooper = 0; squadLooper < Companies[PlayerID].Squads.Length; squadLooper++)
            {
                for (int soldierLooper = 0; soldierLooper < Companies[PlayerID].Squads[squadLooper].Soldiers.Length; soldierLooper++)
                {
                    if ((GameX > Companies[PlayerID].Squads[squadLooper].Soldiers[soldierLooper].X - 25) && (GameX < Companies[PlayerID].Squads[squadLooper].Soldiers[soldierLooper].X + 25))
                    {
                        if ((GameY > Companies[PlayerID].Squads[squadLooper].Soldiers[soldierLooper].Y - 25) && (GameY < Companies[PlayerID].Squads[squadLooper].Soldiers[soldierLooper].Y + 25))
                        {
                            Companies[PlayerID].CurrentlySelectedSquad =squadLooper;
                            return squadLooper;
                        }
                    }
                }
            }

            return -1;
        }

#endregion


        public void GiveOrder(int PlayerID, Order.OrderType newOrder, int SelectedSquad, int GameX, int GameY)
        {

                //the order has been given
                Companies[PlayerID].Squads[SelectedSquad].Order.CurrentOrder = newOrder;
                Companies[PlayerID].Squads[SelectedSquad].Order.OrderPosition = new Point(GameX, GameY);
                Companies[PlayerID].Squads[SelectedSquad].Order.NextOrder = Order.OrderType.Waiting;
                Companies[PlayerID].Squads[SelectedSquad].NewOrder();
                GivingOrder = false;

        }



    }
}
